package com.hina.rec.common.handler;

import cn.idev.excel.write.handler.SheetWriteHandler;
import cn.idev.excel.write.handler.context.SheetWriteHandlerContext;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddressList;

/**
 * 自定义下拉框
 *
 * @author gengkeke
 * @since 2025/4/30
 */
@Slf4j
@Data
public class DropdownSheetWriteHandler implements SheetWriteHandler {

    private String[] listOfValues;

    private int firstRow = 1;
    private int firstCol;
    private int lastCol;

    @Override
    public void afterSheetCreate(SheetWriteHandlerContext context) {
        Sheet sheet = context.getWriteSheetHolder().getSheet();
        CellRangeAddressList range = new CellRangeAddressList(firstRow, 64000, firstCol, lastCol);
        DataValidationHelper helper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = helper.createExplicitListConstraint(listOfValues);
        DataValidation validation = helper.createValidation(constraint, range);
        sheet.addValidationData(validation);
    }
}